﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;
using System.Linq;
using OfficeOpenXml;

namespace ToolExcelPub
{
    public static partial class XTool
    {
        public static void OutCsharpServer()
        {
            if (!OutputCsharpNeed) return;

            ShowInfo("OutCsharpServer!");

            OutCsharpServerClass();
            OutCsharpServerDict();
            OutCsharpServerRead();
        }
        public static void OutCsharpServerClass()
        {
            string path_class = PathCsharp + "\\" + "ConfSvClass.cs";

            var sbw = new StringBuilder();

            sbw.AppendLine(NoteGenerated);
            sbw.AppendLine($"using System.Collections.Generic;");
            sbw.AppendLine(NamespaceServer);
            sbw.AppendLine($"{{");

            foreach (var page in DictPages.Values)
            {
                if (!page.HasType(EToolFileType.服务器))
                {
                    continue;
                }
                var sbnode = new StringBuilder();
                sbnode.AppendLine($"\tpublic class {page.Name} : IConfigBase\t// {page.NameFile}");
                sbnode.AppendLine($"\t{{");
                for (int iii = 0; iii < page.Head.Count; iii++)
                {
                    if (page.Head[iii] == "")
                    {
                        continue;
                    }
                    string tp = page.TypeServer[iii];
                    if (string.IsNullOrEmpty(tp))
                    {
                        continue;
                    }
                    switch (tp)
                    {
                        case "List<string>":
                        case "List<int>":
                        case "List<long>":
                        case "List<float>":
                        case "List<double>":
                        case "List<List<int>>":
                        case "Dictionary<string,string>":
                        case "Dictionary<int,int>":
                        case "Dictionary<long,long>":
                        case "Dictionary<string,int>":
                        case "Dictionary<string, string>":
                        case "Dictionary<int, int>":
                        case "Dictionary<long, long>":
                        case "Dictionary<string, int>":
                            sbnode.AppendLine($"\t\tpublic {tp} {page.Head[iii]} = new(); //{page.HeadC[iii]}");
                            break;
                        default:
                            {
                                var tpServer = tp;
                                if (DictXPclassInfo.ContainsKey(tpServer))
                                {
                                    //tpServer = $"List<{tpServer}>";
                                    sbnode.AppendLine($"\t\tpublic {tpServer} {page.Head[iii]} = new(); //{page.HeadC[iii]}");
                                }
                                else
                                {
                                    sbnode.AppendLine($"\t\tpublic {tpServer} {page.Head[iii]}; //{page.HeadC[iii]}");
                                }
                            }
                            break;
                    }
                }
                string getkey = "Id";
                try { getkey = page.Head[0]; } catch { }
                sbnode.AppendLine($"\t\tpublic object GetKey() {{ return {getkey}; }}");
                sbnode.AppendLine($"\t}}");
                sbw.Append(sbnode);
            }

            sbw.AppendLine($"}}");

            ToolGlobal.WriteNewFile(path_class, sbw.ToString());
        }
        public static void OutCsharpServerDict()
        {
            string path_class = PathCsharp + "\\" + "ConfSvDict.cs";

            var sbw = new StringBuilder();

            sbw.AppendLine(NoteGenerated);
            sbw.AppendLine($"using System.Collections.Generic;");
            sbw.AppendLine($"using UtilLib;");
            sbw.AppendLine(NamespaceServer);
            sbw.AppendLine($"{{");
            sbw.AppendLine($"\tpublic static partial class Conf");
            sbw.AppendLine($"\t{{");

            foreach (var page in DictPages.Values)
            {
                if (!page.HasType(EToolFileType.服务器))
                {
                    continue;
                }
                var sbnode = new StringBuilder();
                sbnode.AppendLine($"\t\t//{page.NameCn}\t// {page.NameFile}");
                var confname = page.Name.Replace("Config", "Conf");
                var keytype = page.TypeServer[0];
                sbnode.AppendLine($"\t\tpublic static Dictionary<{keytype}, {page.Name}> {confname} = new Dictionary<{keytype}, {page.Name}>();");
                sbw.Append(sbnode);
            }

            sbw.AppendLine($"\t}}");
            sbw.AppendLine($"}}");

            ToolGlobal.WriteNewFile(path_class, sbw.ToString());
        }
        public static void OutCsharpServerRead()
        {
            string path_class = PathCsharp + "\\" + "ConfSvRead.cs";

            var sbw = new StringBuilder();

            sbw.AppendLine(NoteGenerated);
            sbw.AppendLine($"using System.Collections.Generic;");
            sbw.AppendLine($"using UtilLib;");
            sbw.AppendLine(NamespaceServer);
            sbw.AppendLine($"{{");
            sbw.AppendLine($"\tpublic static partial class Conf");
            sbw.AppendLine($"\t{{");
            sbw.AppendLine($"\t\tpublic static bool InitConfSettings()");
            sbw.AppendLine($"\t\t{{");

            sbw.AppendLine($"\t\t\tbool result = true;");
            foreach (var page in DictPages.Values)
            {
                if (!page.HasType(EToolFileType.服务器))
                {
                    continue;
                }
                var sbnode = new StringBuilder();
                sbnode.AppendLine($"\t\t\t//{page.NameCn}");
                var confname = page.Name.Replace("Config", "Conf");
                sbnode.AppendLine($"\t\t\tif (result) {{ result = ReadConfig(typeof({page.Name}).Name, ref {confname}); }}");
                sbw.Append(sbnode);
            }
            sbw.AppendLine($"\r\n\t\t\tif (result) {{ result = InitConfSettingsExt(); }}");
            sbw.AppendLine($"\t\t\treturn result;");

            sbw.AppendLine($"\t\t}}");
            sbw.AppendLine($"\t}}");
            sbw.AppendLine($"}}");

            ToolGlobal.WriteNewFile(path_class, sbw.ToString());
        }
    }
}
